// router/articleModel.js
const Router = require("koa-router");
const { TownManager } = require("../../mongodb/manager/town/townManager");
const { verifyToken } = require("../../middleware");
const router = new Router();

/**
 * @swagger
 * tags:
 *   name: Town
 *   description: 小镇接口
 */

// 添加小镇接口
/**
 * @swagger
 * /towns:
 *   post:
 *     summary: 添加小镇
 *     tags: [Town]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               townname:
 *                 type: string
 *                 description: 小镇名称
 *                 example: "凤二客家文创小镇"
 *               midpiclist:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 中间图列表
 *                 example: ["https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town/midpiclist/test-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87-1.jpg", "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town/midpiclist/test-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87-2.jpg", "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town/midpiclist/test-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87-3.jpg","https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town/midpiclist/test-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87-4.jpg"]
 *               pagepiclist:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     name:
 *                       type: string
 *                       example: "风景1"
 *                     url:
 *                       type: string
 *                       example: "https://example.com/轮播图1.jpg"
 *                 description: 轮播图列表
 *                 example:
 *                   - name: "风景1"
 *                     url: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fpagepiclist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%871.jpg"
 *                   - name: "风景2"
 *                     url: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fpagepiclist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%872.jpg"
 *                   - name: "风景3"
 *                     url: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fpagepiclist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%873.jpg"
 *                   - name: "风景4"
 *                     url: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fpagepiclist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%874.jpg"
 *                   - name: "风景5"
 *                     url: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fpagepiclist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%875.jpg"
 *                   - name: "风景6"
 *                     url: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fpagepiclist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%876.jpg"
 *               spotlist:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 景点图列表
 *                 example: ["https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fspotlist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87-%E5%85%A8%E6%99%AF%E5%9B%BE.jpg", "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fspotlist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87-%E5%87%A4%E4%BA%8C%E6%9D%91%E5%8F%B2%E9%A6%86.jpg", "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Fspotlist%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87-%E5%87%A4%E5%AE%B4%E5%B9%BF%E5%9C%BA.jpg"]
 *               tel1:
 *                 type: string
 *                 example: "123-456-7890"
 *               tel2:
 *                 type: string
 *                 example: "234-567-8901"
 *               tel3:
 *                 type: string
 *                 example: "345-678-9012"
 *               townbackground:
 *                 type: string
 *                 description: 背景图片
 *                 example: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Ftownbackground%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87.jpg"
 *               townlogo:
 *                 type: string
 *                 description: 小镇 logo
 *                 example: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Ftownlogo%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87.png"
 *               townvideo:
 *                 type: string
 *                 description: 小镇视频
 *                 example: "https://nfu-town-1328412748.cos.ap-guangzhou.myqcloud.com/town%2Ftownvideo%2Ftest-%E5%87%A4%E4%BA%8C%E5%AE%A2%E5%AE%B6%E6%96%87%E5%88%9B%E5%B0%8F%E9%95%87.mp4"
 *     responses:
 *       200:
 *         description: 创建成功
 *       400:
 *         description: 请求参数不合法
 */
router.post("/", verifyToken("admin"), async (ctx) => {
  // TODO: 考虑参数校验的问题
  try {
    const town = await TownManager.createTown(ctx.request.body);
    ctx.status = 200;
    ctx.body = town;
  } catch (error) {
    ctx.status = 400;
    ctx.body = { message: error.message };
  }
});

// 修改小镇接口
/**
 * @swagger
 * /towns:
 *   put:
 *     summary: 根据小镇名称修改小镇信息
 *     tags: [Town]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - townname
 *               - updateData
 *             properties:
 *               townname:
 *                 type: string
 *                 description: 小镇名称
 *                 example: "凤二客家文创小镇"
 *               updateData:
 *                 type: object
 *                 description: 更新的数据对象
 *                 additionalProperties:
 *                   type: string  # 或根据具体的更新属性进行定义
 *                 example:
 *                   tel1: "098-765-4321"
 *                   townlogo: "https://example.com/new-logo.png"
 *     responses:
 *       200:
 *         description: 修改成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "修改成功"
 *                 updatedTown:
 *                   type: object
 *                   description: 修改后的小镇对象
 *                   example:
 *                     townname: "阳光小镇"
 *                     tel1: "098-765-4321"
 *                     townlogo: "https://example.com/new-logo.png"
 *       400:
 *         description: 请求参数不合法
 *       404:
 *         description: 找不到小镇
 */
router.put("/", verifyToken("admin"), async (ctx) => {
  const { townname } = ctx.request.body; // 查询字段
  const { updateData } = ctx.request.body; // 更新字段

  let town;

  // 查找小镇
  if (townname) {
    // 根据小镇名字查找小镇
    town = await TownManager.getTownByTownname(townname);
  } else {
    ctx.status = 400;
    ctx.body = { message: "Townname is required." };
    return;
  }

  // 更新小镇
  try {
    const updatedTown = await TownManager.updateTown(town._id, updateData);
    ctx.status = 200;
    ctx.body = { message: "修改成功", updatedTown };
  } catch (error) {
    ctx.status = 404;
    ctx.body = { message: error.message };
  }
});

// 删除小镇接口
/**
 * @swagger
 * /towns:
 *   delete:
 *     summary: 根据小镇名称删除小镇
 *     tags: [Town]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               townname:
 *                 type: string
 *                 description: 小镇名称
 *             example:
 *               townname: "阳光小镇"
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "Town successfully marked as deleted."
 *       400:
 *         description: 缺少必需参数
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "townname is required."
 *       404:
 *         description: 找不到小镇
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "Town not found."
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "Failed to mark user as deleted."
 */
router.delete("/", verifyToken("admin"), async (ctx) => {
  const { townname } = ctx.request.body; // 查询字段

  // 检查请求体，确保至少有一个参数
  if (!townname) {
    ctx.status = 400;
    ctx.body = { message: "Townname is required." };
    return;
  }

  let town;

  // 根据小镇名称查找小镇
  town = await TownManager.getTownByTownname(townname);

  // 如果用户不存在，返回错误
  if (!town) {
    ctx.status = 404;
    ctx.body = { message: "Town not found" };
    return;
  }

  // 使用已存在的标记删除方法
  try {
    await TownManager.softDeleteUser(town._id);
    ctx.status = 200;
    ctx.body = { message: "Town successfully marked as deleted." };
  } catch (error) {
    ctx.status = 500; // 处理可能的服务器错误
    ctx.body = { message: "Failed to mark user as deleted." };
  }
});

// 获取所有小镇列表接口
/**
 * @swagger
 * /towns/list:
 *   get:
 *     summary: 获取所有小镇列表
 *     tags: [Town]
 *     responses:
 *       200:
 *         description: 小镇列表
 *       500:
 *         description: 服务器错误
 */
router.get("/list", async (ctx) => {
  try {
    const towns = await TownManager.getAllActiveTowns();
    ctx.status = 200;
    ctx.body = towns;
  } catch (error) {
    ctx.status = 500;
    ctx.body = { message: error.message };
  }
});

// 获取所有小镇名字列表接口
/**
 * @swagger
 * /towns/list-name:
 *   get:
 *     summary: 获取所有小镇名字列表
 *     tags: [Town]
 *     responses:
 *       200:
 *         description: 小镇名字列表
 *       500:
 *         description: 服务器错误
 */
router.get("/list-name", async (ctx) => {
  try {
    const towns = await TownManager.getAllActiveTowns();
    // 将小镇名称映射为对象数组，每个对象的键名为 'townname'
    const townNames = towns.map((town) => ({ townname: town.townname }));

    ctx.set("Content-Type", "application/json");

    ctx.status = 200;
    ctx.body = townNames; // 返回 JSON 对象数组
  } catch (error) {
    ctx.status = 500;
    ctx.body = { message: error.message };
  }
});

module.exports = { router };
